package core

import (
	"blogx_server/global"
	"blogx_server/models"
	"blogx_server/models/enum"
	"blogx_server/utils/pwd"
	"github.com/sirupsen/logrus"
)

func InitRoot() {
	var rootUser models.UserModel
	err := global.DB.Take(&rootUser, "username = ?", "root").Error
	if err != nil {
		hashPwd, _ := pwd.GenerateFromPassword("Qwe123..")
		// 创建用户
		err = global.DB.Create(&models.UserModel{
			Username:       "root",
			Nickname:       "杰哥",
			RegisterSource: enum.RegisterTerminalSourceType,
			Password:       hashPwd,
			Role:           enum.AdminRole,
		}).Error
		if err != nil {
			logrus.Infof("root用户创建失败")
			return
		}
		logrus.Infof("root用户创建成功")
		return
	}
	logrus.Infof("root用户已存在")
}
